from rest_framework_jwt.authentication import get_authorization_header
from automatic_door_opening.settings import JWT_AUTH
from rest_framework_jwt.serializers import VerifyJSONWebTokenSerializer


def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'code': 200,
        'msg': 'success',
        'data': {
            'token': token
        }
    }


def jwt_get_user_secret(user):
    # print(user.user_secret)
    return user.user_secret


def jwt_get_user(request):
    jwt_value = get_authorization_header(request).decode().split(' ')[1]
    data = {JWT_AUTH['JWT_AUTH_HEADER_PREFIX']: jwt_value}
    valid_data = VerifyJSONWebTokenSerializer().validate(data)
    return valid_data['user']
